#include "chooselevelscene.h"
#include <QPixmap>
#include <QMenuBar>
#include <QMenu>
#include <QAction>
#include <QPainter>
#include "mypushbutton.h"
#include <QTimer>
#include <QLabel>


ChooseLevelScene::ChooseLevelScene(QWidget *parent) : QMainWindow(parent)
{
    //设置固定窗口大小
    this->setFixedSize(320,588);
    //设置图标
    this->setWindowIcon(QPixmap(":/res/Coin0001.png"));
    //设置标题
    this->setWindowTitle("选择关卡");

    //创建菜单栏
    QMenuBar *bar = this->menuBar();
    this->setMenuBar(bar);
    //创建开始菜单
    QMenu *startMenu = bar->addMenu("开始");
    //创建按钮菜单项
    QAction *quitAction = startMenu->addAction("退出");
    //点击退出
    connect(quitAction,&QAction::triggered,[=](){
        this->close();//退出
    });


    //创建返回按钮
    MyPushButton *backBtn = new MyPushButton(":/res/BackButton.png",":/res/BackButtonSelected.png");
    backBtn->setParent(this);
    backBtn->move(this->width()-backBtn->width(),this->height()-backBtn->height());
    connect(backBtn,&MyPushButton::clicked,[=](){
        QTimer::singleShot(500,this,[=](){
            backSound->play();
            this->hide();
            //触发自定义信号
            emit this->chooseSceneBack();
        });
    });

    playScene = NULL;
    //创建选择关卡按钮
    for(int i=0;i<20;i++){
        MyPushButton *menuBtn = new MyPushButton(":/res/LevelIcon.png");
        menuBtn->setParent(this);
        menuBtn->move(25 + (i%4)*70 , 130+ (i/4)*70);//其实这一行就是循环布局的意思
        QLabel *label = new QLabel;
        label->setParent(this);
        label->setFixedSize(menuBtn->width(),menuBtn->height());
        label->setText(QString::number(i+1));
        label->setAlignment(Qt::AlignHCenter|Qt::AlignVCenter);//设置字体上下左右居中
        label->move(25 + (i%4)*70 , 130+ (i/4)*70);
        label->setAttribute(Qt::WA_TransparentForMouseEvents,true);//鼠标事件穿透
        connect(menuBtn,&MyPushButton::clicked,[=](){//跳转到翻金币场景
            if(playScene==NULL){
                chooseSound->play();//选择关卡音效
                this->hide();
                playScene = new PlayScene(i+1);
                playScene->setGeometry(this->geometry());
                playScene->show();

                connect(playScene,&PlayScene::chooseSenceBack,[=](){
                    this->setGeometry(playScene->geometry());
                    this->show();
                    delete playScene;
                    playScene = NULL;

                });
            }


        });

    }


}

void ChooseLevelScene::paintEvent(QPaintEvent *event){
    QPainter painter(this);
    //绘制背景
    QPixmap pixmap;
    pixmap.load(":/res/OtherSceneBg.png");
    painter.drawPixmap(0,0,this->width(),this->height(),pixmap);

    //加载标题
    pixmap.load(":/res/Title.png");
    painter.drawPixmap(this->width()-pixmap.width()*0.5,30,pixmap.width(),pixmap.height(),pixmap);

}
